
package smpl.auth.google;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.acegisecurity.GrantedAuthority;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;
/**
 * TODO need check for further request to storage server.
 * @author managanesh
 *
 */
public class GoogleUserAuthentication implements UserAuthentication {

	private  HttpClient httpClient=null;
	private  HttpMethod httpMethod=null;
	private AuthRecord gAuthrec=null;
	private List<NameValuePair> data;

	private static final Logger log=Logger.getLogger("user.authentication");
	public String doAuthentication() {
		log.debug("Google doAuthentication Begin");

		NameValuePair[] tmp=new NameValuePair[data.size()];
		httpMethod.setQueryString(data.toArray(tmp));

		try{
			httpClient.executeMethod(httpMethod);
			log.debug("Google User Authenticated");

		}catch(Exception e){
			log.error("Google User Authentication Failed", e);
		}

		httpMethod.getResponseHeader(AuthConstants.GOOGLE_AUTH_RESPONSE_HEADER_ERROR);
		httpMethod.getStatusCode();
		InputStream responseBody=null;
		Properties responseProp=new Properties();

		try {

			responseBody=httpMethod.getResponseBodyAsStream();
			responseProp.load(responseBody);
			if(!responseProp.contains("Error")) return "SUCCESS";

		} catch (IOException e) {
			log.error("Google Response body got exception", e);
		}


		log.debug("google doAuthentication End");
		return responseProp.getProperty("Error", "");
	}

	/* (non-Javadoc)
	 * @see org.acegisecurity.Authentication#getAuthorities()
	 */
	public GrantedAuthority[] getAuthorities() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see org.acegisecurity.Authentication#getCredentials()
	 */
	public Object getCredentials() {
		// TODO Auto-generated method stub
		return null;
	}

	public Object getDetails() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see org.acegisecurity.Authentication#getDetails()
	 */

	/* (non-Javadoc)
	 * @see java.security.Principal#getName()
	 */
	public String getName() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see org.acegisecurity.Authentication#getPrincipal()
	 */
	public Object getPrincipal() {
		// TODO Auto-generated method stub
		return null;
	}

	public void init(AuthRecord rec) {

		log.debug("Google User Initialization Begin");
		httpClient=new HttpClient();
		httpMethod=new PostMethod(AuthConstants.GOOGLE_AUTH_URL);
		gAuthrec=rec;
		data=new ArrayList<NameValuePair>();
		data.add(new NameValuePair("accountType", AuthConstants.GOOGLE_AUTH_TYPE));
		data.add(new NameValuePair("Email", gAuthrec.getUsername()));
		data.add(new NameValuePair("Passwd", gAuthrec.getPassword()));
		data.add(new NameValuePair("service", "mail"));
		data.add(new NameValuePair("source", "sampurnastorage-google"));

		log.debug("Google User Initialization End");
	}

	/* (non-Javadoc)
	 * @see org.acegisecurity.Authentication#isAuthenticated()
	 */
	public boolean isAuthenticated() {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see org.acegisecurity.Authentication#setAuthenticated(boolean)
	 */
	public void setAuthenticated(boolean arg0) throws IllegalArgumentException {
		// TODO Auto-generated method stub

	}


}


